package com.android.geotrack.user;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.android.geotrack.R;
import com.android.geotrack.R.drawable;
import com.android.geotrack.R.id;
import com.android.geotrack.R.layout;
import com.android.geotrack.artist.ArtistsActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class UserActivity extends Activity
{
	private String user;
	
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Bundle b = this.getIntent().getExtras();
		user = b.getString("user");
		setContentView(R.layout.user);
		Log.v("Activity creation", "UserActivity");
		
		TextView user = (TextView) findViewById(R.id.userDetail);
		ImageView image = (ImageView) findViewById(R.id.imageDetail);
		TextView distance = (TextView) findViewById(R.id.distanceDetail);
		TextView age = (TextView) findViewById(R.id.ageDetail);
		TextView score = (TextView) findViewById(R.id.scoreDetail);
		
		// load details
		user.setText(this.user);
		distance.setText("Distance: " + b.getString("distance") + " km");
		age.setText("Last updated: " + b.getString("age") + " minutes ago");
		score.setText("Score: " + b.getString("score") + " %");
		
		// load image
		String thumb = b.getString("image");
		Drawable d = null;
		if (thumb!=null && ! thumb.equals("")) {
			image.setScaleType(ImageView.ScaleType.CENTER_CROP);
			try {
				d = Drawable.createFromStream(new URL(thumb).openStream(), "user.jpg");
			} catch (MalformedURLException e) {
				Log.e("MalformedURLException", e.toString());
			} catch (IOException e) {
				Log.e("IOException", e.toString());
			}
		} else {
			d = getResources().getDrawable(R.drawable.user);
		}
		image.setImageDrawable(d);
		
		// related artists button
		final Button relatedArtistsButton = (Button) findViewById(R.id.relatedArtistsButton);
		relatedArtistsButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
        		Intent i = new Intent(UserActivity.this, ArtistsActivity.class);
        		i.putExtra("user", UserActivity.this.user);
        		i.setFlags(i.FLAG_ACTIVITY_NEW_TASK);
        		UserActivity.this.startActivity(i);
            }
        });
		
		// back button
		final Button backButton = (Button) findViewById(R.id.userBackButtonDetail);
		backButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {            	
            	finish();
            }
        });
	}
}